package com.sd.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.content.Context;


import com.sd.app.AppInit;
import com.sd.database.BookInfo;

public class BookInfoHandle {

	private int currentstate = -1;
	private final int BOOK = 0;
	private final int BOOKNAME = 1;
	private final int WORDNUMBER = 2;
	private final int CHAPTERNUMBER = 3;
	private final int COVERURL = 4;
	private final int SOURCE = 5;
	private final int ABOUTAUTHOR = 6;
	private final int ABOUTBOOK = 7;
	private final int AUTHROR = 8;
	private final int BOOKSERIAL = 9;

	public Vector<BookInfo> getBooksInfo(Context context){
		Vector<BookInfo> books = new Vector<BookInfo>();
		//InputStream is= context.getResources().openRawResource(R.raw.booksinfo);
		InputStream is = null;
		try {
			is = new FileInputStream(AppInit.getInstance().APP_BOOKS+"booksinfo.xml");
		} catch (FileNotFoundException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		XmlPullParserFactory xmlPullParserFactory;
		XmlPullParser xmlPullParser = null;
		try {
			xmlPullParserFactory = XmlPullParserFactory.newInstance();
			xmlPullParserFactory.setNamespaceAware(true);
			xmlPullParser = xmlPullParserFactory.newPullParser();
		} catch (XmlPullParserException e1) {
			e1.printStackTrace();
		}
		BookInfo book = null;
		boolean isItemTAG = false;
		try {
			xmlPullParser.setInput(is, "utf-8");

			int eventType = xmlPullParser.getEventType();
			while (eventType != XmlPullParser.END_DOCUMENT) {

				if (eventType == XmlPullParser.START_DOCUMENT) {
					System.out.println("start Document...");
				} else if (eventType == XmlPullParser.END_DOCUMENT) {
					System.out.println("end Document...");
				} else if (eventType == XmlPullParser.START_TAG) {
					if (xmlPullParser.getName().equals("book")) {
						book = new BookInfo();
						isItemTAG = true;
					}
					if (xmlPullParser.getName().equals("book_name")) {
						currentstate = BOOKNAME;
					}
					if (xmlPullParser.getName().equals("word_number")) {
						currentstate = WORDNUMBER;
					}
					if (xmlPullParser.getName().equals("chapter_number")) {
						currentstate = CHAPTERNUMBER;
					}
					if (xmlPullParser.getName().equals("cover_url")) {
						currentstate = COVERURL;
					}
					if (xmlPullParser.getName().equals("source")) {
						currentstate = SOURCE;
					}
					if (xmlPullParser.getName().equals("about_author")) {
						currentstate = ABOUTAUTHOR;
					}
					if (xmlPullParser.getName().equals("about_book")) {
						currentstate = ABOUTBOOK;
					}
					if (xmlPullParser.getName().equals("author")) {
						currentstate = AUTHROR;
					}
					if(xmlPullParser.getName().equals("book_serial")){
						currentstate = BOOKSERIAL;
					}
				} else if (eventType == XmlPullParser.END_TAG) {
					if (xmlPullParser.getName().equals("book")) {
						books.add(book);
					}

				} else if (eventType == XmlPullParser.TEXT) {
					if (isItemTAG) {
						switch (currentstate) {
						case BOOKNAME:
							System.out.println(xmlPullParser.getText());
							book.bookName = xmlPullParser.getText();
							currentstate = -1;
							break;
						case WORDNUMBER:
							book.wordNumber = xmlPullParser.getText();
							currentstate = -1;
							break;
						case CHAPTERNUMBER:
							book.chapterNumber = xmlPullParser.getText();
							currentstate = -1;
							break;
						case COVERURL:
							book.coverUrl = xmlPullParser.getText();
							currentstate = -1;
							break;
						case SOURCE:
							book.source = xmlPullParser.getText();
							currentstate = -1;
							break;
						case ABOUTAUTHOR:
							book.aboutAuthor = xmlPullParser.getText();
							currentstate = -1;
							break;
						case ABOUTBOOK:
							book.aboutBook = xmlPullParser.getText();
							currentstate = -1;
							break;
						case AUTHROR:
							book.author = xmlPullParser.getText();
							currentstate = -1;
							break;
						case BOOKSERIAL:
							System.out.println(xmlPullParser.getText());
							book.bookSerial = xmlPullParser.getText();
							currentstate = -1;
							break;
						default:
							break;
						}
					}
				}

				eventType = xmlPullParser.next();

			}

		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return books;
	}
}
